import { getPlayListDetail, getSongDetail, getSubscribersPlaylist, getCommentPlaylist, getRelatedPlaylist } from '../../api/services/api'
export default ({
    namespaced: true,
    state: {
        playListDetail: {
            playlist: {}
        },
        loading: false,
        currentType: {
            currentPage: 1,
            limmit: 20,
        },
        musicList: [],
        likeList: [],
        hotCommentsList: [],
        recommended: [],
    },
    mutations: {
        //加载动画
        changLoading(state, val) {
            state.loading = !state.loading;
        },
        changePlayListDetail(state, val) {
            state.playListDetail = val.playlist;
        },
        changeMusicList(state, val) {
            state.musicList = val;
        },
        changeLikeList(state, val) {
            state.likeList = val;
        },
        changeHotCommentsList(state, val) {
            state.hotCommentsList = val;
        },
        changeRecommended(state, val) {
            state.recommended = val;
            // console.log('666666666666', state.recommended);
        },


    },
    actions: {
        //获取歌单详情
        async getPlayListDetail(context, val) {
            context.commit('changLoading');
            const data = await getPlayListDetail(val);
            // console.log('判断判断', data);
            if (data.code == 200) {
                context.commit('changePlayListDetail', data);
                context.dispatch('getSongDetail', data)
                context.dispatch('getSubscribersPlaylist', data.playlist.id);
                context.dispatch('getCommentPlaylist', data.playlist.id);
                context.dispatch('getRelatedPlaylist', data.playlist.id);
                context.commit('changLoading');
            }
        },

        async getSongDetail(context, val) {
            const trackIds = val.playlist.trackIds;
            var listid = '';
            trackIds.forEach((item, index) => {
                listid += item.id + ','
            })
            const data = await getSongDetail(listid.slice(0, listid.lastIndexOf()));
            // console.log('歌单列表', data);
            context.commit('changeMusicList', data.songs);
            // console.log('歌曲列表', data.songs);
        },
        //获取喜欢该歌单的人
        async getSubscribersPlaylist(context, val) {
            const data = await getSubscribersPlaylist(val);
            // console.log('喜欢该歌单', data);
            context.commit('changeLikeList', data.subscribers);
        },
        //获取精彩评论
        async getCommentPlaylist(context, val) {
            const data = await getCommentPlaylist(val);
            // console.log('评论', data);
            context.commit('changeHotCommentsList', data.hotComments);
        },
        //相关歌单推荐
        async getRelatedPlaylist(context, val) {
            const data = await getRelatedPlaylist(val);
            // console.log('相关推荐', data);
            context.commit('changeRecommended', data.playlists)
        }

    },

})
